<?php

namespace App\Models;

use App\Libraries\Common\CommonFun;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class RsUsers extends Model
{
    public const CREATED_AT = NULL;

    public const UPDATED_AT = NULL;

    protected $fillable = [
        'user_id',
        'loginname',
        'username',
        'create_time'
    ];

    use HasFactory;
    // protected $connection = 'mysql_pms';
     protected $table='rs_user';
    protected $connection = "mysql"; //config/database.php中的connections数组中的
    public $timestamps=false;
    //查询数据
    public static function lists(){
        $where=[];
        //$where[]=['username','!=','null'];
        $query=self::where($where);
        $count=$query->count();
        $data = $query->orderBy('user_id', 'desc')->get()->map(function (RsUsers $rebate) use ($count) {
            $rebate = $rebate->attributesToArray();
            //$rebate['username']=User::query()->where('id','=',$rebate['user_id'])->first()->getAttribute('username');
            //$rebate['is_status']=$rebate['group_id'] !=null?'已绑':'解绑';
            // $rebate['is_show_home'] = $rebate['is_show_home']==0 ?'否' : '是';

            return $rebate;
        })->toArray();

        return $data ? compact('count', 'data') : false;

    }

    //获取用户列表
    public static function listss($page = 1, $limit = 20, $params = []){
        $where=[];
        $username=data_get($params,'name',false);
        if($username){
            $where[]=['username','like','%'.$username.'%'];
        }

        //$where[]=['username','!=','null'];
        $query=self::where($where);
        $count=$query->count();
        $data = $query->offset(CommonFun::getOffset($page, $limit))->limit($limit)->orderBy('id', 'desc')->get()->map(function (RsUsers $rebate) use ($count) {
            $rebate = $rebate->attributesToArray();
            //$rebate['username']=User::query()->where('id','=',$rebate['user_id'])->first()->getAttribute('username');
           // $rebate['is_status']=$rebate['group_id'] !=null?'已绑':'解绑';
            // $rebate['is_show_home'] = $rebate['is_show_home']==0 ?'否' : '是';

            return $rebate;
        })->toArray();

        return $data ? compact('count', 'data') : false;

    }

    //获取pms库 user表数据
    public function getPmsUser(){
        $connection = 'pms';
        return $this->setConnection($connection)->belongsTo(User::class,'user_id','id');
    }




}
